package com.glowdayz.glowmee.bc;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.glowmee.cmd.AdminTableCMD;

public class DeleteFileBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
    public DeleteFileBC() {
        super();
    }
	
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	DBHandler.getInstance().connect();
		
		String sql = "SELECT fileDir, fileSaveName FROM attachefile";
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		ArrayList<AdminTableCMD> result = new ArrayList<AdminTableCMD>();
		
		try {
			ResultSet rs = pstmt.executeQuery();
			if (rs != null) {
				while(rs.next()) {
					String boardType = rs.getString(1);
					String boardText = rs.getString(2);
					AdminTableCMD item = new AdminTableCMD(boardType, boardText);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {}
		
		for (int i=0; i<result.size(); i++) {
			AdminTableCMD item = result.get(i);
			String fileName = item.getBoardText();
			
			String file1 = fileName.replace(".", "_100.");
			File f = new File(item.getBoardType() + "/" + file1);
			if (f.exists()) {
				f.delete();
			}
			
			String file2 = fileName.replace(".", "_160.");
			File f2 = new File(item.getBoardType() + "/" + file2);
			if (f2.exists()) {
				f2.delete();
			}
			
			String file3 = fileName.replace(".", "_320.");
			File f3 = new File(item.getBoardType() + "/" + file3);
			if (f3.exists()) {
				f3.delete();
			}
			
			String file4 = fileName.replace(".", "_360.");
			File f4 = new File(item.getBoardType() + "/" + file4);
			if (f4.exists()) {
				f4.delete();
			}
			
			String file5 = fileName.replace(".", "_720.");
			File f5 = new File(item.getBoardType() + "/" + file5);
			if (f5.exists()) {
				f5.delete();
			}
		}
		
		DBHandler.getInstance().disconnect();
    		
	}
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    }

}
